package org.leiyang.common.proxy;

import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import reactivefeign.webclient.client.WebReactiveHttpClient;

@Component
public class ReactiveHttpClientBeanPostProcessor<P extends Publisher<?>> implements BeanPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(ReactiveHttpClientBeanPostProcessor.class);
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> beanType = AopUtils.getTargetClass(bean);
        log.info(beanType.getName());
        if(WebReactiveHttpClient.class.isAssignableFrom(beanType)) {
            WebReactiveHttpClient<P> httpClient = (WebReactiveHttpClient<P>) bean;
            return new WebReactiveHttpClientWrapper<P>(httpClient);
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
